package com.tvunetworks.center.device.controller;

import com.tvunetworks.center.common.constant.LocalConstant;
import com.tvunetworks.center.common.model.LoginUser;
import com.tvunetworks.center.common.util.Result;
import com.tvunetworks.center.common.util.ResultUtil;
import com.tvunetworks.center.device.annotation.Logined;
import com.tvunetworks.center.device.service.PidPortMappingService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Etan Yang
 * @create: 2021/10/13 19:33
 */

@RestController
@RequestMapping("/pidPortMapping")
public class PidPortMappingController {
    @Autowired
    private PidPortMappingService pidPortMappingService;

    /**
     * 初始化tvu_device到t_pid_port_mapping，条件是type=R，且有IP
     * @return  success
     */
    @RequestMapping("initDeviceExistIpToPidPortMapping")
    public Result initDeviceExistIpToPidPortMapping() {
        pidPortMappingService.initDeviceExistIpToPidPortMapping();
        return ResultUtil.success();
    }

    /**
     * 将t_pid_port_mapping表中的在线或live且domain为空的设备获取对应的domain并更新对应的domain
     *
     * @return map，key为SUCCESS，对应的value为成功的个数，key为FAILED，对应的value为失败个数，key为FailedPeerId，对应的value为失败的pid集合
     */
    @RequestMapping("initOnlineOrLiveReceiverDomainToPidPortMapping")
    public Result initOnlineOrLiveReceiverDomainToPidPortMapping() {
        return ResultUtil.success(pidPortMappingService.initOnlineOrLiveReceiverDomainToPidPortMapping());
    }

    /**
     * 设备上线下线上报消息
     *
     * @param message 上报的消息
     * @return 返回执行结果
     */
    @RequestMapping("receiverStatusChanged")
    public Result receiverStatusChanged(@RequestParam("message") String message) {
        pidPortMappingService.receiverStatusChanged(message);
        return ResultUtil.success();
    }

    /**
     * 刷新数据库中的数据到cache中
     * @return cache
     */
    @RequestMapping("refreshPirPortMappingCache")
    public Result refreshPirPortMappingCache() {
        pidPortMappingService.refreshPirPortMappingCache();
        return ResultUtil.success();
    }

    /**
     * 绑定peerId的Ip和域名
     * @param loginUser 登录用户
     * @param peerId    设备id
     * @return 返回结果
     */
    @PostMapping("refreshIpAndDomain")
    public Result refreshIpAndDomain(@Logined LoginUser loginUser, @RequestParam("peerId") String peerId) {
        if (loginUser == null) {
            return ResultUtil.noLogin();
        }
        if (!StringUtils.equalsIgnoreCase(loginUser.getRole(), LocalConstant.UserRole.SUPER_ADMIN)
                && !StringUtils.equalsIgnoreCase(loginUser.getRole(), LocalConstant.UserRole.SUPPORT)) {
            return ResultUtil.noAuthority();
        }
        pidPortMappingService.refreshIpAndDomain(peerId, loginUser.getEmail());
        return ResultUtil.success();
    }

    /**
     * 根据peerId获取PidPortMapping
     *
     * @param loginUser 登录用户
     * @param peerId    设备id
     * @return 返回结果
     */
    @PostMapping("getByPeerId")
    public Result getByPeerId(@Logined LoginUser loginUser, @RequestParam("peerId") String peerId) {
        if (loginUser == null) {
            return ResultUtil.noLogin();
        }
        if (!StringUtils.equalsIgnoreCase(loginUser.getRole(), LocalConstant.UserRole.SUPER_ADMIN)
                && !StringUtils.equalsIgnoreCase(loginUser.getRole(), LocalConstant.UserRole.SUPPORT)) {
            return ResultUtil.noAuthority();
        }

        return ResultUtil.success(pidPortMappingService.getByPeerId(peerId));
    }
}
